<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="姓名" prop="customer_name">
        <el-input v-model="form.customer_name" placeholder="请填写姓名" />
      </el-form-item>
      <el-form-item label="联系方式" prop="customer_mobile">
        <el-input v-model="form.customer_mobile" placeholder="请填写手机号" />
      </el-form-item>
      <el-form-item label="证件号" prop="id_card">
        <el-input
          v-model="form.id_card"
          placeholder="请填写证件号"
          @input="changeIdcard"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { addCustomer, editCustomer } from '@/api/rent'
  import { isPhone, isIdCard } from '@/utils/validate'
  export default {
    name: 'CustomerEdit',
    data() {
      const validatePhone = (rule, value, callback) => {
        if (!isPhone(value)) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
      }
      const validateIdCard = (rule, value, callback) => {
        if (value && !isIdCard(value)) {
          callback(new Error('请输入正确的身份证号'))
        } else {
          callback()
        }
      }
      return {
        type: '',
        form: {
          customer_name: '',
          customer_mobile: '',
          id_card: '',
        },
        rules: {
          customer_name: [
            { required: true, trigger: 'blur', message: '请输入姓名' },
          ],
          customer_mobile: [
            { required: true, trigger: 'blur', message: '请输入手机号' },
            { validator: validatePhone, trigger: 'blur' },
          ],
          id_card: [{ validator: validateIdCard, trigger: 'change' }],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    methods: {
      showEdit(type, e) {
        this.type = type
        if (type == 'add') {
          this.title = '添加同住人'
          this.form.room_id = e.room_id
          this.form.rent_sn = e.rent_sn
          this.form.customer_type = 2
        } else if (type == 'edit') {
          this.title = '编辑'
          this.form.customer_id = e.id
          this.form.customer_name = e.customer_name
          this.form.customer_mobile = e.customer_mobile
          this.form.id_card = e.customer_idcard
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      changeIdcard(e) {
        if (e == '') {
          setTimeout(() => {
            this.$refs['form'].clearValidate('customer_idcard')
          }, 200)
        }
      },
      save() {
        this.$refs['form'].validate((valid) => {
          if (valid) {
            this.edit()
          }
        })
      },
      async edit() {
        if (this.type == 'add') {
          // 添加
          const { data } = await addCustomer(this.form)
          if (data.is_success) {
            this.$baseMessage('添加成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          }
        } else if (this.type == 'edit') {
          // 修改
          const { data } = await editCustomer(this.form)
          if (data.is_success) {
            this.$baseMessage('修改成功', 'success', 'vab-hey-message-success')
            this.$emit('fetch-data')
            this.close()
          } else if (!data.is_success) {
            this.close()
          }
        }
      },
    },
  }
</script>
<style lang="scss" scoped>
  .inline {
    display: flex;
    flex-wrap: wrap;
    .el-form-item {
      display: inline-block;
      vertical-align: top;
      .el-input {
        width: 160px;
      }
    }
    .daterange {
      width: 32px;
      line-height: 32px;
      text-align: center;
    }
  }
</style>
